home *** CD-ROM | disk | FTP | other *** search
/ Programmers Heaven 2 / Programmers Heaven 2.iso / files / graphics / library / wgt51_r2.zip / WGT5 / EXAMPLES / WGT38.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-03  |  12.7 KB  |  576 lines

  1. /*
  2. ==============================================================================
  3.               WordUp Graphics Toolkit Version 5.0                     
  4.                  Demonstration Program 38                         
  5.                                           
  6.  Shows how to use the communications library.          
  7.  WLINK.CFG must contain the correct information before you can use this                                                                              
  8.  program.  You should modify the file to suit your baud rate and com port.
  9.  
  10.  
  11.  *** PROJECT ***                                                             
  12.  This program requires the file WGT5_WC.LIB and WCOMM_WC.LIB to be linked.                      ║
  13.                                           
  14.  *** DATA FILES ***                                                          
  15.  WLINK.CFG                                                                       
  16.                                WATCOM C++ VERSION 
  17. ==============================================================================
  18. */
  19.  
  20. #include <stdio.h>
  21. #include <conio.h>
  22. #include <dos.h>
  23. #include <stdarg.h>
  24. #include <wlink.h>
  25. #include <wgt5.h>
  26.  
  27. /* Multiplater Link data */
  28. int localplayer = -1, remoteplayer = -1;  /* Either 0 or 1 if connected */
  29.  
  30.  
  31.  
  32. /* Text mode routines */
  33.  
  34. /* Text mode drawing */
  35. #define SCREENPTR       (char*)0xB8000
  36. #define SCREENWIDTH     80
  37. #define SCREENHEIGHT    25
  38.  
  39. #define BLACK           0
  40. #define BLUE            1
  41. #define GREEN           2
  42. #define CYAN            3
  43. #define RED             4
  44. #define MAGENTA         5
  45. #define BROWN           6
  46. #define LIGHTGRAY       7
  47. #define DARKGRAY        8
  48. #define LIGHTBLUE       9
  49. #define LIGHTGREEN      10
  50. #define LIGHTCYAN       11
  51. #define LIGHTRED        12
  52. #define LIGHTMAGENTA    13
  53. #define YELLOW          14
  54. #define WHITE           15
  55. #define BLINK           128
  56.  
  57.  
  58.  
  59. static char *textptr = SCREENPTR;
  60. static char textcolor = BLACK<<4+LIGHTGRAY;
  61. int cursorx, cursory;
  62. int textscroll = 1;
  63. int linewrap = 1;
  64.  
  65. /* Sets the current foreground and background colors */
  66. void setcolor (int forecolor, int backcolor)
  67. {
  68.  textcolor = ((backcolor & 0x0F) << 4) + (forecolor & 0x0F);
  69. }
  70.  
  71.  
  72. /* Sets the cursor position */
  73. void set_cursor_pos (short x, short y)
  74. {
  75. union REGS r;
  76.  
  77.  r.h.ah = 2;
  78.  r.h.bh = 0;
  79.  r.h.dh = y;
  80.  r.h.dl = x;
  81.  int386 (0x10, &r, &r);
  82. }
  83.  
  84. /* Makes a new starting pointer to the text screen */
  85. void gotoxy (int x, int y)
  86. {
  87.  textptr = SCREENPTR + 2*x + 160*y;
  88.  cursorx = x;
  89.  cursory = y;
  90. }
  91.  
  92. void advance_cursor (void)
  93. {
  94.  cursorx++;
  95.  if ((cursorx == SCREENWIDTH) && (linewrap))
  96.   {
  97.    cursory++;
  98.    cursorx = 0;
  99.   }
  100. }
  101.  
  102. /* Draws a single character directly to the screen */
  103. void outchar (char c)
  104. {
  105.  switch (c) {
  106.   case 0:
  107.   case 1:
  108.   case 2:
  109.   case 3:
  110.   case 4:
  111.   case 5:
  112.   case 6: break;
  113.   case 7: sound (400);  /* Bell */
  114.       delay (20);
  115.       sound (200);
  116.       delay (20);
  117.       nosound ();
  118.       break;
  119.   case 8: if (cursorx > 0)              /* Backspace */
  120.         gotoxy (cursorx-1, cursory);
  121.       break;
  122.   case 9: advance_cursor ();    /* Tab */
  123.       advance_cursor ();  
  124.       advance_cursor ();
  125.       advance_cursor ();
  126.       break;
  127.   case 10: gotoxy (0, cursory+1); /* Line feed */
  128.        break;
  129.   case 11:
  130.   case 12: break;
  131.   case 13: gotoxy (0, cursory+1);         /* Carriage return */
  132.        break;
  133.   case 14:
  134.   case 15:
  135.   case 16:
  136.   case 17:
  137.   case 18:
  138.   case 19:
  139.   case 20:
  140.   case 21:
  141.   case 22:
  142.   case 23:
  143.   case 24:
  144.   case 25:
  145.   case 26:
  146.   case 27:
  147.   case 28:
  148.   case 29:
  149.   case 30:
  150.   case 31: break;
  151.   default: *textptr++ = c;
  152.        *textptr++ = textcolor;
  153.        advance_cursor ();
  154.        break;
  155.   }
  156.  
  157.  /* Scroll the screen if needed */
  158.  if ((cursory >= SCREENHEIGHT) && (textscroll))
  159.   {
  160.    scroll_text_up ();
  161.    textbox (0, 24, 79, 24, ' ');
  162.    gotoxy (0, SCREENHEIGHT-1);
  163.   }
  164.  
  165. }
  166.  
  167. /* Draws a character a number of times in a row */
  168. void repchar (char c, int times)
  169. {
  170.  while (--times >= 0)
  171.    {
  172.     *textptr++ = c;
  173.     *textptr++ = textcolor;
  174.     advance_cursor ();
  175.    }
  176. }
  177.  
  178.  
  179. /* Draws a string of text */
  180. void outtext (char *string)
  181. {
  182.  while (*string)
  183.    outchar (*string++);
  184. }
  185.  
  186.  
  187. /* Scrolls the screen up */
  188. void scroll_text_up (void)
  189. {
  190. char *source;
  191. char *dest;
  192.  
  193.  gotoxy (0, 0);
  194.  dest = textptr;
  195.  source = textptr + 160;
  196.  memcpy (dest, source, SCREENWIDTH * (SCREENHEIGHT-1) * 2);
  197. }
  198.  
  199.  
  200.  
  201. /* Draws a filled text box */
  202. void textbox (int x1, int y1, int x2, int y2, char c)
  203. {
  204. int width, height;
  205.  
  206.  width = x2 - x1 + 1;
  207.  height = y2 - y1 + 1;
  208.  
  209.  while (--height >= 0)
  210.   {
  211.    gotoxy (x1,y1++);
  212.    repchar (c, width);
  213.   }
  214. }
  215.  
  216. /* Prints a string with formatting */
  217. void tprintf (char *fmt, ...)
  218. {
  219. va_list  argptr;                     /* Argument list pointer*/
  220. char str[251];                       /* Buffer to build string into*/
  221.  
  222.  va_start (argptr, fmt);              /* Initialize va_ functions*/
  223.  
  224.  vsprintf (str, fmt, argptr);         /* prints string to buffer*/
  225.  outtext (str);
  226.  /* Send string in graphics mode */
  227.  
  228.  va_end (argptr);                     /* Close va_ functions*/
  229. }
  230.  
  231.  
  232. /* End of text mode routines */
  233.  
  234. /* Reads the modem configuration from a file */
  235. void read_config (char *filename)
  236. {
  237. char line[257];
  238. short length;
  239. FILE *config;
  240. short i;
  241. unsigned char *vectorptr;
  242. unsigned long lnum;
  243.  
  244.  /* Set up the initial values */
  245.  link_info.baud_rate = -1;
  246.  link_info.com_port  = -1;
  247.  link_info.irq_num   = -1;
  248.  link_info.uart      = -1;
  249.  link_info.vector    = -1;
  250.  
  251.  if ((config = fopen (filename, "r")) == NULL)
  252.   {
  253.    printf ("Could not open wlink.cfg file.");
  254.    exit(1);
  255.   }
  256.  
  257.  /* Read the modem initialation string */
  258.  fgets (line, 256, config);
  259.  strcpy (link_info.modem_init, line);
  260.  length = strlen (link_info.modem_init) - 1;
  261.  if (link_info.modem_init [length] == '\n')
  262.      link_info.modem_init [length] = '\0';
  263.  
  264.  /* Read the modem hangup string */
  265.  fgets (line, 256, config);
  266.  strcpy (link_info.modem_hangup, line);
  267.  length = strlen (link_info.modem_hangup) - 1;
  268.  if (link_info.modem_hangup [length] == '\n')
  269.      link_info.modem_hangup [length] = '\0';
  270.  
  271. /* Read the baud rate */
  272.  fgets (line, 256, config);
  273.  sscanf (line, "%lu", &link_info.baud_rate);
  274.  if (link_info.baud_rate != 9600  && link_info.baud_rate != 14400 &&
  275.      link_info.baud_rate != 19200 && link_info.baud_rate != 38400 &&
  276.      link_info.baud_rate != 57600 && link_info.baud_rate != 115200)
  277.     link_info.baud_rate = 9600;
  278.  /* Default to 9600 baud */
  279.  
  280.  /* Read the com port */
  281.  fgets (line, 256, config);
  282.  sscanf (line, "%hi", &link_info.com_port);
  283.  printf ("read %hi\n", link_info.com_port);
  284.  if (link_info.com_port < 1 || link_info.com_port > 4)
  285.      link_info.com_port = 1;
  286.  
  287.  /* Read the IRQ */
  288.  fgets (line, 256, config);
  289.  sscanf (line, "%hi", &link_info.irq_num);
  290.  
  291.  fclose (config);
  292.  
  293.  /* If the IRQ or the uart is not known yet, search for it */
  294.  if ((link_info.irq_num == -1) || (link_info.uart == -1))
  295.     wlink_getuart ();
  296.  printf ("Found UART at port 0x%x, irq %d...\n\n",link_info.uart,link_info.irq_num);
  297.  
  298.  /* Search for the interrupt vector if it wasn't set */
  299.  if (link_info.vector == -1)
  300.     wlink_getvector ();
  301.  
  302.  if (link_info.vector == WLINK_NOVECTOR)
  303.    {
  304.     printf ("Warning: no NULL or iret interrupt vectors were found in the 0x60 to 0x66\nrange.  Aborting.\n");
  305.     exit (1);
  306.    }
  307.  
  308. }
  309.  
  310.  
  311. /* Print all the communications data for debugging */
  312. void print_link_config (void)
  313. {
  314.  setcolor (WHITE, BLUE);
  315.  textbox (0, 0, 79, 24, ' ');
  316.  
  317.  gotoxy (30, 0);
  318.  tprintf ("WGT Communications Demo");
  319.  gotoxy (2, 2);
  320.  tprintf("\n\n\n");
  321.  tprintf("MODEM INIT STRING  : %s\n", link_info.modem_init);
  322.  tprintf("MODEM HANGUP STRING: %s\n", link_info.modem_hangup);
  323.  tprintf("BAUD RATE:           %lu\n",link_info.baud_rate);
  324.  tprintf("COM PORT:            %i\n", link_info.com_port);
  325.  tprintf("IRQ NUMBER:          %i\n", link_info.irq_num);
  326.  tprintf("UART:                %i\n", link_info.uart);
  327.  tprintf("VECTOR:              %i\n", link_info.vector);
  328. }
  329.  
  330.  
  331. void print_main_menu (void)
  332. {
  333.  gotoxy (0, 15);
  334.  tprintf ("1. Call (Modem)\n");
  335.  tprintf ("2. Answer (Modem)\n");
  336.  tprintf ("3. Call (Direct)\n");
  337.  tprintf ("4. Answer (Direct)\n");
  338.  tprintf ("5. Quit\n");
  339.  tprintf ("Selection:");
  340.  set_cursor_pos (10, 20);
  341. }
  342.  
  343.  
  344. /* Send anything typed on the keyboard, and show anything received on the
  345.    screen */
  346. void terminal_mode (void)
  347. {
  348. int c;
  349. char inkey;
  350.  
  351.  linewrap = 0;
  352.  textbox (0, 0, 79, 24, ' ');
  353.  gotoxy (0, 0);
  354.  tprintf ("Terminal mode...  Press ESC to quit");
  355.  gotoxy (0, 2);
  356.  while (1)
  357.   {
  358.    /* Check for keypress */
  359.    if (_bios_keybrd (1))
  360.     {
  361.      if ((inkey = _bios_keybrd (0) & 0xff) == 27)
  362.      {
  363.       /* ESC key -- Exit terminal mode -- Flush both sides */
  364.       while (wlink_read_byte () != -1);
  365.       while (_bios_keybrd (1))
  366.      _bios_keybrd (0);
  367.       printf ("\n\n");
  368.       return;
  369.      }
  370.     wlink_write_buffer (&(unsigned char)inkey, 1);
  371.    }
  372.   /* Check for received byte */
  373.  c = wlink_read_byte ();
  374.  
  375.  if (c != -1)
  376.    outchar (c);
  377.  
  378.  }
  379. }
  380.  
  381.  
  382. /* Split screen chat */
  383. void talk_mode (void)
  384. {
  385. int c;
  386. char inkey;
  387. int rx, ry, lx, ly;
  388.  
  389.  linewrap = 1;
  390.  setcolor (WHITE, BLUE);
  391.  textbox (0, 0, 79, 24, ' ');
  392.  setcolor (CYAN, BLUE);
  393.  textbox (0, 0, 79, 0, ' ');
  394.  textbox (0, 12, 79, 13, ' ');
  395.  
  396.  gotoxy (0, 1);
  397.  repchar ('▓', 80);
  398.  gotoxy (0, 12);
  399.  repchar ('═', 80);
  400.  gotoxy (0, 14);
  401.  repchar ('▓', 80);
  402.  
  403.  gotoxy (34, 0);
  404.  tprintf ("Remote User");
  405.  gotoxy (34, 13);
  406.  tprintf ("Local User");
  407.  
  408.  setcolor (WHITE, BLUE);
  409.  
  410.  lx = 0;        /* Local cursor coordinates */
  411.  ly = 15;
  412.  rx = 0;        /* Remote cursor coordinates */
  413.  ry = 2;
  414.  set_cursor_pos (lx, ly);
  415.  textscroll = 0;  /* turn off text scrolling */
  416.  
  417.  while (1)
  418.   {
  419.    /* Check for keypress */
  420.    if (_bios_keybrd (1))
  421.     {
  422.      if ((inkey = _bios_keybrd (0) & 0xff) == 27)
  423.      {
  424.       /* ESC key -- Exit terminal mode -- Flush both sides */
  425.       while (wlink_read_byte () != -1);
  426.       while (_bios_keybrd (1))
  427.      _bios_keybrd (0);
  428.       printf ("\n\n");
  429.       return;
  430.      }
  431.     wlink_write_buffer (&(unsigned char)inkey, 1);
  432.     
  433.     gotoxy (lx, ly);    /* Set the local coordinates */
  434.     outchar (inkey);        /* show the character you typed */
  435.     lx = cursorx;       /* Get the new cursor coords */
  436.     ly = cursory;
  437.     if (ly == SCREENHEIGHT)
  438.       ly = 15;
  439.     if (lx == 0)       /* Erase the line that was previously here */
  440.        textbox (0, ly, 79, ly, ' ');
  441.     set_cursor_pos (lx, ly);
  442.    }
  443.   /* Check for received byte */
  444.  c = wlink_read_byte ();
  445.  
  446.  if (c != -1)
  447.   {  
  448.    gotoxy (rx, ry);    /* Set the remote coordinates */
  449.    outchar (c);        /* show the character you typed */
  450.    rx = cursorx;       /* Get the new cursor coords */
  451.    ry = cursory;
  452.    if (ry == 12)
  453.       ry = 2;
  454.    if (rx == 0)        /* Erase the line that was previously here */
  455.      textbox (0, ry, 79, ry, ' ');
  456.      
  457.   }
  458.    
  459.  }
  460.  
  461.  textscroll = 1;  /* Turn text scrolling back on */
  462.  
  463. }
  464.  
  465. void dial_number (void)
  466. {
  467. char dialstring[80];
  468.  
  469.  textbox (0, 0, 79, 24, ' ');
  470.  gotoxy (0, 0);
  471.  tprintf ("Enter the number to dial:");
  472.  set_cursor_pos (26, 0);
  473.  scanf ("%s", dialstring);
  474.  
  475.  if (wlink_dial (dialstring))
  476.   {  /* connected */
  477.    localplayer = 0;
  478.    remoteplayer = 1;
  479.   }
  480. }
  481.  
  482.  
  483. void wait_for_call (void)
  484. {
  485.  textbox (0, 0, 79, 24, ' ');
  486.  gotoxy (0, 0);
  487.  tprintf ("Waiting for call...");
  488.  
  489.  if (wlink_answer ())
  490.   {  /* connected */
  491.    localplayer = 1;
  492.    remoteplayer = 0;
  493.   }
  494. }
  495.  
  496.  
  497. void process_main_menu (void)
  498. {
  499. char c;
  500. int quit;
  501.  
  502.   quit = 0;
  503.  
  504.   do {
  505.    c = getch ();
  506.  
  507.    if (c == '1')
  508.     {
  509.      dial_number ();
  510.      terminal_mode ();
  511.      quit = 1;
  512.     }
  513.    else if (c == '2')
  514.     {
  515.      wait_for_call ();
  516.      terminal_mode ();
  517.      quit = 1;
  518.     }
  519.    else if (c == '3')
  520.     {
  521.      talk_mode ();
  522.      quit = 1;
  523.     }
  524.    else if (c == '4')
  525.     {
  526.      talk_mode ();
  527.      quit = 1;
  528.     }
  529.    else if (c == '5')
  530.     {
  531.      
  532.      quit = 1;
  533.     }
  534.  
  535.  
  536.  } while (quit == 0);
  537. }
  538.  
  539.  
  540.  
  541.  
  542. /* A simple terminal program */
  543. void main (void)
  544. {
  545.  printf ("WGT Example #38\n\n");
  546.  printf ("Uses the communications library to either call a BBS or talk\n");
  547.  printf ("directly across a null modem connection.  For a null modem connection\n");
  548.  printf ("both computers should be running the program and one person will\n");
  549.  printf ("dial while the other answers.\n");
  550.  printf ("** You must modify WLINK.CFG before running this program **\n");
  551.  printf ("\n\nPress any key to continue.\n");
  552.  getch ();
  553.  
  554.  
  555.  read_config ("wlink.cfg");
  556.  wlink_initport ();
  557.  
  558.  print_link_config ();
  559.  
  560.  if (link_info.uarttype == WLINK_16550_UART)
  561.    tprintf ("UART is a 16550");
  562.  else
  563.    tprintf ("UART is an 8250");
  564.  
  565.  print_main_menu ();
  566.  process_main_menu ();
  567.  
  568.  wlink_shutdownport ();
  569.  wsetmode (3);
  570. }
  571.  
  572.  
  573.  
  574.  
  575.  
  576.